/*
 * Created by SharpDevelop.
 * User: michaelo
 * Date: 03/08/2006
 * Time: 11:01
 * 
 * To change this template use Tools | Options | Coding | Edit Standard Headers.
 */

using System;
using System.Collections;
using System.Drawing;
using System.Windows.Forms;
using Orz.LogManager.Config;

namespace Orz.LogManager
{
	/// <summary>
	/// Description of ImportLogFileForm.
	/// </summary>
	public class ImportLogFileForm : System.Windows.Forms.Form
	{
		private System.Windows.Forms.Panel panel1;
		private System.Windows.Forms.Splitter splitter1;
		private System.Windows.Forms.TreeView tvwFiles;
		private System.Windows.Forms.TextBox txtPreview;
		private System.Windows.Forms.Button cmdImport;
		private System.Windows.Forms.Button cmdCancel;
		
		private logsetLogfile logfile_i;
		
		
		
		public ImportLogFileForm()
		{
			//
			// The InitializeComponent() call is required for Windows Forms designer support.
			//
			InitializeComponent();
			
			//
			// TODO: Add constructor code after the InitializeComponent() call.
			//
			InitTreeview();
			
			PopulatePreviewPane();
				
		}
		
		
		
		#region Windows Forms Designer generated code
		/// <summary>
		/// This method is required for Windows Forms designer support.
		/// Do not change the method contents inside the source code editor. The Forms designer might
		/// not be able to load this method if it was changed manually.
		/// </summary>
		private void InitializeComponent() {
			this.cmdCancel = new System.Windows.Forms.Button();
			this.cmdImport = new System.Windows.Forms.Button();
			this.txtPreview = new System.Windows.Forms.TextBox();
			this.tvwFiles = new System.Windows.Forms.TreeView();
			this.splitter1 = new System.Windows.Forms.Splitter();
			this.panel1 = new System.Windows.Forms.Panel();
			this.panel1.SuspendLayout();
			this.SuspendLayout();
			// 
			// cmdCancel
			// 
			this.cmdCancel.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Right)));
			this.cmdCancel.DialogResult = System.Windows.Forms.DialogResult.Cancel;
			this.cmdCancel.Location = new System.Drawing.Point(392, 40);
			this.cmdCancel.Name = "cmdCancel";
			this.cmdCancel.TabIndex = 5;
			this.cmdCancel.Text = "Cancel";
			this.cmdCancel.Click += new System.EventHandler(this.CmdCancelClick);
			// 
			// cmdImport
			// 
			this.cmdImport.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Right)));
			this.cmdImport.Enabled = false;
			this.cmdImport.Location = new System.Drawing.Point(392, 8);
			this.cmdImport.Name = "cmdImport";
			this.cmdImport.TabIndex = 4;
			this.cmdImport.Text = "Import";
			this.cmdImport.Click += new System.EventHandler(this.CmdImportClick);
			// 
			// txtPreview
			// 
			this.txtPreview.Dock = System.Windows.Forms.DockStyle.Fill;
			this.txtPreview.Location = new System.Drawing.Point(211, 0);
			this.txtPreview.Multiline = true;
			this.txtPreview.Name = "txtPreview";
			this.txtPreview.ReadOnly = true;
			this.txtPreview.Size = new System.Drawing.Size(173, 375);
			this.txtPreview.TabIndex = 4;
			this.txtPreview.Text = "Preview:";
			// 
			// tvwFiles
			// 
			this.tvwFiles.Dock = System.Windows.Forms.DockStyle.Left;
			this.tvwFiles.ImageIndex = -1;
			this.tvwFiles.Location = new System.Drawing.Point(0, 0);
			this.tvwFiles.Name = "tvwFiles";
			this.tvwFiles.SelectedImageIndex = -1;
			this.tvwFiles.Size = new System.Drawing.Size(208, 375);
			this.tvwFiles.TabIndex = 0;
			this.tvwFiles.AfterSelect += new System.Windows.Forms.TreeViewEventHandler(this.TvwFilesAfterSelect);
			// 
			// splitter1
			// 
			this.splitter1.Location = new System.Drawing.Point(208, 0);
			this.splitter1.Name = "splitter1";
			this.splitter1.Size = new System.Drawing.Size(3, 375);
			this.splitter1.TabIndex = 3;
			this.splitter1.TabStop = false;
			// 
			// panel1
			// 
			this.panel1.Anchor = ((System.Windows.Forms.AnchorStyles)((((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom) 
						| System.Windows.Forms.AnchorStyles.Left) 
						| System.Windows.Forms.AnchorStyles.Right)));
			this.panel1.Controls.Add(this.txtPreview);
			this.panel1.Controls.Add(this.splitter1);
			this.panel1.Controls.Add(this.tvwFiles);
			this.panel1.Location = new System.Drawing.Point(0, 0);
			this.panel1.Name = "panel1";
			this.panel1.Size = new System.Drawing.Size(384, 375);
			this.panel1.TabIndex = 3;
			// 
			// ImportLogFileForm
			// 
			this.AcceptButton = this.cmdImport;
			this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
			this.CancelButton = this.cmdCancel;
			this.ClientSize = new System.Drawing.Size(480, 375);
			this.Controls.Add(this.cmdCancel);
			this.Controls.Add(this.cmdImport);
			this.Controls.Add(this.panel1);
			this.Name = "ImportLogFileForm";
			this.StartPosition = System.Windows.Forms.FormStartPosition.CenterParent;
			this.Text = "Import a Log File";
			this.panel1.ResumeLayout(false);
			this.ResumeLayout(false);
		}
		#endregion
		
		
		public logsetLogfile LogFile
		{
			get { return logfile_i; }
		}
		
		
		void CmdCancelClick(object sender, System.EventArgs e)
		{
			logfile_i = null;
			this.Close();
		}
		
		
		
		void InitTreeview()
		{
			IList logsets = LogsetLoader.GetLogsets();

			tvwFiles.Nodes.Clear();
		
			IEnumerator e = logsets.GetEnumerator();
		
			while (e.MoveNext())
			{
				LogsetWithFile l = (LogsetWithFile) e.Current;
				AddLogsetToTree (l);
			}	
		
		}
		
		
		
		void AddLogsetToTree(LogsetWithFile l)
		{
			TreeNode n = tvwFiles.Nodes.Add(l.FileName);
			
			foreach (logsetLogfile f in l.Logset.logfile)
			{
				TreeNode n2 = n.Nodes.Add(f.name);
				n2.Tag = f;
			}
		}
		
		
		void PopulatePreviewPane()
		{
			txtPreview.Text = "Preview:" + "\r\n";
			
			if (null != logfile_i)
			{
				txtPreview.Text += "Name: " + logfile_i.name + "\r\n";
				txtPreview.Text += "Connection type: " + GetLogType(logfile_i) + "\r\n";;
				txtPreview.Text += "File path: " + logfile_i.path + "\r\n";
			}
			
			cmdImport.Enabled = (null != logfile_i);
		}
		
		
		
		void TvwFilesAfterSelect(object sender, System.Windows.Forms.TreeViewEventArgs e)
		{
			if (null != e.Node.Parent)
			{
				MessageBox.Show(e.Node.Text);
				
				logfile_i =(logsetLogfile) e.Node.Tag;					 											
			}
			else
			{
				logfile_i = null;
			}
			
			PopulatePreviewPane();	
		}
		
		void CmdImportClick(object sender, System.EventArgs e)
		{
			this.Close();
		}
	
		string GetLogType(logsetLogfile l)
		{			
			if (l.telnetProtocol.Equals(protocolType.ssh))
			{
				return "SSH";
			}
			else if (l.telnetProtocol.Equals(protocolType.telnet))
			{
				return "Telnet";
			}
			else
			{
				return "Local file";
			}
		}
	}
}
